package org.java.jdk8Stream;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 测试JDK8 stream流
 * @Date 2024/8/15 15:37
 * @Created by 10574
 */
@Slf4j
public class Jdk8StreamDemo {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    private static class User {
        private String name;
        private String phone;
    }

    public static void main(String[] args) {
        testAnyAndAllAndNoneMatch();
    }

    private static void testAnyAndAllAndNoneMatch() {
        final User user1 = new User("李", "1223123123");
        final User user2 = new User("哈哈", "412412412");
        final User user3 = new User("haha", "231231");
        final User user4 = new User("李", "4241241241");
        final List<User> list1 = new ArrayList<User>();
        list1.add(user1);
        list1.add(user2);
        list1.add(user3);
        list1.add(user4);
        final List<User> list2 = new ArrayList<>();
        list2.add(new User("haha", "2131231"));
        list2.add(new User("李", "231231"));
        // anyMatch: 只要有一个条件满足即返回true, 可获得交集
        log.info("交集:{}", Arrays.toString(list1.stream()
                .filter(user -> list2.stream().anyMatch(user5 -> user.getName().equals(user5.getName()))).toArray()));
        // allMatch: 全部满足条件才能返回true
        log.info("allMatch:{}", Arrays.toString(list1.stream()
                .filter(user -> list2.stream().allMatch(user5 -> user.getName().equals(user5.getName()))).toArray()));
        // noneMatch: 全都不满足才会返回true
        log.info("差集:{}", Arrays.toString(list1.stream()
                .filter(user -> list2.stream().noneMatch(user5 -> user.getName().equals(user5.getName()))).toArray()));
    }
}
